<!--
 * @Author: Terry
 * @Date: 2022-11-01 18:06:44
 * @LastEditors: Terry
 * @LastEditTime: 2024-11-27 16:01:50
 * @Description: file content
 * @FilePath: \auxface_web\src\components\dialogPopUpV2.vue
-->
<template>
  <transition  name="el-zoom-in-center" @leave="handleLeave" @enter="handleEnter">
    <div class="AUX-preview-main" v-show="this.$store.state.dialogPopUpShow"   
    :style="popStyle"
    >
      <div class="AUX-common-page-header">
        <el-page-header @back="goBack" :content="title" />
        <div class="options">
            <el-button v-if="!disabled && ruleForm.uuid != undefined" type="primary" @click="handleConfirm('update','put')">
              {{$t('global.update')}}</el-button>
          <el-button v-else-if="!disabled" type="primary" @click="handleConfirm('create','post')">
            {{$t('global.create')}}</el-button>
          <el-button  v-else  type="primary" @click="goBack">{{$t('global.confirm')}}</el-button>
          <el-button @click="goBack">  {{$t('global.cancel')}}</el-button>
        </div>
      </div>
      <div class="preview-main" v-loading="formLoading" :element-loading-text=" $t('global.loading')">  
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="m-t-15"
          label-position="right"
        >
          <slot></slot>
        </el-form>
      </div>
    </div>
  </transition>
</template>
<style lang="scss">

</style>
<script>
import axios from "@/utils/axios.js";
export default {
  props: {
    ruleForm: {
      type: Object,
      default: () => ({})
    },
    disabled: {
      type: Boolean,
      default: false
    },
    rules: {
      type: Object,
      default: () => ({})
    },
    editHas: {
      type: String,
      default:'',
    },
  },
  name: "dialogPopUp",
  data() {
    return {
      formLoading:false,
      controller:'',
      func:'',
      title:'',
      dialogShow:false,
      popStyle:{},
    }
  },
  methods: {
    setPostion(){
      const container = document.querySelector('.el-main');
      var top = container.scrollTop -15;
      var height = container.clientHeight + 20;
      this.popStyle = {
        'top': `${top}px`,
        'height': `${height}px`,
        'max-height': `${height}px`,
      }
 
    },
    changeController(controller){
      this.controller = controller;
    },
    changeTitle(title){
      this.title = title;
    },
    changeFunc(func){
      this.func = func
    },
    show(){
      this.$store.commit('changeDialogPopUpShow', true);
      this.setPostion();
    },
    // 返回取消
    goBack() {
      this.$store.commit('changeDialogPopUpShow', false);
    },
    // 添加修改
    handleConfirm(type,method) {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          var func  =  this.func || type,
          api = this.module + "/admin/v1/" + this.controller + '/' + func,
          data = {...this.ruleForm};
          this.load = this.loading();
          axios[method](api, data )
            .then((res) => {
              data = {...data,...res.data};
              this.$emit('submit', data );
            })
            .catch(res=>{
              res.message && this.$message.error(res.message);
            })
            .finally(() => {
              this.load.close()
            });
       
        }
      })
    },
    handleLeave(){
      this.$store.commit('changePopUp',true);
      this.$refs.ruleForm?.resetFields()
    },
    handleEnter(){
      this.$store.commit('changePopUp', false);
    }
  },
  mounted(){
    var listUrl = this.$route.path.split("/").filter((item) => {
        return item != "modules" && item != "";
      });
      
      this.module = listUrl[0];
       //安全培训需要修改
      if(listUrl && listUrl[0]=='corpSafetytrain'){
        this.module = 'safetytrain';
        this.controller = listUrl[1]
      }else if(listUrl[0]=='platform'){
        this.module = 'platform';
        this.controller = listUrl[1]
      }else{
        this.module = listUrl[0];
        if(listUrl.length==2){
          this.controller = listUrl[1]
        }else if(listUrl.length>2){
          if(this.module=='video'){
            this.controller = listUrl[1]
          }else{
            this.controller = listUrl[2]
          }
        }
      }
      this.title = this.$t(`${this.module}.router.${this.controller}`);
      const container = document.querySelector('.el-main');
  }
}
</script>
  